import java.sql.*;
import java.util.Scanner;#我是注释#

class StudentManagement {
private Connection conn;

public StudentManagement() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
conn = DriverManager.getConnection(url, "root", "password");
} catch (Exception e) {
e.printStackTrace();
}
}

public void addStudent(String name, String id, double math, double english, double java, double computer) {
try {
String sql = "INSERT INTO students (name, id, math, english, java, computer) VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, name);
pst.setString(2, id);
pst.setDouble(3, math);
pst.setDouble(4, english);
pst.setDouble(5, java);
pst.setDouble(6, computer);
pst.executeUpdate();
pst.close();
System.out.println("学生信息添加成功！");
} catch (SQLException e) {
e.printStackTrace();
}
}

public void queryStudent(String name) {
try {
String sql = "SELECT * FROM students WHERE name = ?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, name);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
System.out.println("姓名：" + rs.getString("name"));
System.out.println("学号：" + rs.getString("id"));
System.out.println("数学成绩：" + rs.getDouble("math"));
System.out.println("英语成绩：" + rs.getDouble("english"));
System.out.println("Java成绩：" + rs.getDouble("java"));
System.out.println("计算机成绩：" + rs.getDouble("computer"));
}
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
StudentManagement sm = new StudentManagement();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生姓名：");
String name = scanner.nextLine();
System.out.println("请输入学生学号：");
String id = scanner.nextLine();
System.out.println("请输入数学成绩：");
double math = scanner.nextDouble();
System.out.println("请输入英语成绩：");
double english = scanner.nextDouble();
System.out.println("请输入Java成绩：");
double java = scanner.nextDouble();
System.out.println("请输入计算机成绩：");
double computer = scanner.nextDouble();
sm.addStudent(name, id, math, english, java, computer);
sm.queryStudent(name);
}
}